package com.wwdeng.print.app.printer

import android.app.Activity
import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.IBinder
import android.util.Log
import net.posprinter.posprinterface.IMyBinder
import net.posprinter.posprinterface.TaskCallback
import net.posprinter.service.PosprinterService
import net.posprinter.utils.PosPrinterDev

/**
 * PrintTestDemo
 * @Author：wwdeng
 * @CreateTime：2025/4/25 01:21
 * <pre>
 *
 * </pre>
 */
object PrintSDK {
    /**
     * 打印binder
     */
    var printBinder: IMyBinder? = null
    var isConnected = false

    private var serconnection = object: ServiceConnection {
        override fun onServiceConnected(name: ComponentName, service: IBinder) {
            printBinder = service as IMyBinder
            printBinder?.SetAutoRecon(false)
            Log.e("TAG", "Service Connected")
        }

        override fun onServiceDisconnected(name: ComponentName) {
            Log.e("TAG", "Service Disconnected")
        }
    }

    /**
     * 初始化绑定服务
     */
    fun initPrintService(act: Activity){
        //bind service，get imyBinder
        val intent = Intent(act, PosprinterService::class.java)
        act.bindService(intent, serconnection, Activity.BIND_AUTO_CREATE)
    }

    /**
     * 连接打印机
     */
    fun onConnectPrintAction(ip: String,callback: TaskCallback?){
        if (ip.isEmpty()){
            Log.e("TAG", "IP is failed!")
            return
        }
        printBinder?.ConnectNetPort(ip,9100,object: TaskCallback {
            override fun OnSucceed() {
                isConnected = true
                Log.e("TAG", "Connect Success")
                callback?.OnSucceed()
            }

            override fun OnFailed() {
                isConnected = false
                Log.e("TAG", "Connect Failed")
                callback?.OnFailed()
            }
        })
    }

    /**
     * 断开连接
     */
    fun onDisConnectPrintAction(callback: TaskCallback?){
        printBinder?.DisconnectCurrentPort(object : TaskCallback{
            override fun OnSucceed() {
                isConnected = false
                Log.e("TAG", "DisConnect Success")
                callback?.OnSucceed()
            }
            override fun OnFailed() {
                Log.e("TAG", "DisConnect Failed")
                callback?.OnFailed()
            }
        })
    }

    /**
     * 获取打印机状态
     */
    fun getPrinterStatus(): PosPrinterDev.PrinterStatus?{
        if (isConnected){
            return printBinder?.GetPrinterStatus()
        }
        return null
    }

    /**
     * 开始打印
     */
    fun onPrintAction(list: MutableList<ByteArray>,callback: TaskCallback?){
        if (!isConnected){
            Log.e("TAG", "Connect Failed")
            callback?.OnFailed()
            return
        }
        printBinder?.WriteSendData(object: TaskCallback{
            override fun OnSucceed() {
                Log.e("TAG", "Print Success")
                callback?.OnSucceed()
            }

            override fun OnFailed() {
                Log.e("TAG", "Print Failed")
                callback?.OnFailed()

            }
        }) { list }
    }
}